package com.cloud.pole.shop.controller.goods.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.rest.BaseController;
import com.cloud.pole.shop.biz.goods.ShopGoodsBiz;
import com.cloud.pole.shop.biz.goods.ShopGoodsPromotionBiz;
import com.cloud.pole.shop.model.entity.goods.ShopGoods;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsPromotion;
import com.cloud.pole.shop.model.vo.goods.ShopGoodsPromotionAdminVo;
import com.cloud.pole.shop.service.goods.GoodsPromotionService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

/**
 * 
* <p>Title: MenuController</p>  
* <p>Description: </p>  
* @author hsg  
* @date 2019年2月27日
 */
@Controller
@RequestMapping("/goods/promotion/admin_merch")
@Slf4j
public class GoodsPromotionAdminMerchantController extends BaseController<ShopGoodsPromotionBiz, ShopGoodsPromotion> {
    @Autowired
    private GoodsPromotionService promotionService;
	
    @RequestMapping(value = "/getPageList", method = RequestMethod.GET)
    @ResponseBody
    public ApiRestResponse<ShopGoodsPromotionAdminVo> getPageList(
      	  @RequestHeader(defaultValue = "") String token,
    	  @RequestParam(defaultValue = "1") int page,
  		  @RequestParam(defaultValue = "10") int limit,
  		  @RequestParam(defaultValue = "") String goodsName,
  		  @RequestParam(defaultValue = "") String shopId,
  		  @RequestParam(defaultValue = "") String shopsName,
  		  @RequestParam(defaultValue = "") String promotionType) {
    	try {
    		goodsName = goodsName.trim();
    		shopsName = shopsName.trim();
			Map<String, Object> map = new HashMap<>();
			map.put("goodsName", goodsName);
			map.put("shopsName", shopsName);
			if(!StringUtils.isEmpty(shopId)) {
				map.put("shopId", shopId);
			}
			map.put("promotionType", promotionType);
			PageInfo<ShopGoodsPromotionAdminVo> pageInfo = promotionService.selectShopGoodsPromotionAdminVoListByMap(token, map, page, limit);
			return ApiRestResponse.successOfPage(CommonConstants.STATUS_OK_MESSAGE, (int)pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageSize(),
					pageInfo.getList());
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @RequestMapping(value = "/deletePromotionBatch", method = RequestMethod.DELETE)
    @ResponseBody
    public ApiRestResponse<Boolean> deletePromotionBatch(
        	@RequestHeader(defaultValue = "") String token,
    	    @RequestParam(defaultValue = "") String promotionIds) {
    	try {
    		log.info("promotionIds["+promotionIds+"]");
			ParamException.err(StringUtils.isBlank(promotionIds), "promotionIds不能为空");
    		promotionService.deletePromotionBatch(token, promotionIds);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, true);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @RequestMapping(value = "/deletePromotion/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ApiRestResponse<Boolean> deletePromotion(
    		@RequestHeader(defaultValue = "") String token,
    		@PathVariable int id) {
    	try {
    		promotionService.deletePromotion(token, id);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, true);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @RequestMapping(value = "/addPromotion", method = RequestMethod.POST)
    @ResponseBody
    public ApiRestResponse<Boolean> addPromotion(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestBody ShopGoodsPromotion entity) {
    	try {
    		promotionService.addPromotion(token, entity);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, true);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @RequestMapping(value = "/modifyPromotion", method = RequestMethod.PUT)
    @ResponseBody
    public ApiRestResponse<Boolean> modifyPromotion(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestBody ShopGoodsPromotion entity) {
    	try {
    		promotionService.modifyPromotion(token, entity);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, true);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
